package com.azt.provider.impl;

import com.azt.api.pojo.ProHotFloor;
import com.azt.api.pojo.ProHotProduct;
import com.azt.api.pojo.ProShopHotProduct;
import com.azt.api.pojo.*;
import com.azt.api.service.ProCategoryService;
import com.azt.api.service.ProHotFloorService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.ProHotFloorMapper;
import com.azt.provider.mapper.ProHotProductMapper;
import com.azt.provider.mapper.ProShopHotProductMapper;
import com.azt.model.page.TPagination;
import com.azt.provider.mapper.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class ProHotFloorServiceImpl implements ProHotFloorService{
	@Autowired
    private ProHotFloorMapper proHotFloorMapper;

	@Autowired
	private ProHotProductMapper proHotProductMapper;

	@Autowired
	private ProductMapper productMapper;

	@Autowired
	private ProSkuMapper proSkuMapper;

	@Autowired
	private ProShopHotProductMapper proShopHotProductMapper;

	@Override
	public List<ProHotFloor> getHotProductList(Map<String,String> hotFloor) {
		List<ProHotFloor> getHotProductList=proHotFloorMapper.getHotProductList(hotFloor);
		return getHotProductList;
	}

	@Override
	public TPagination<ProHotProductShow> listHotProduct(int page, int size, Map<String, Object> condition) {
		int count = proHotProductMapper.selectCountCondition(condition);
		TPagination<ProHotProductShow> pagin = new TPagination<ProHotProductShow>();
		pagin.setPageNo(page);
		pagin.setPageSize(size);
		pagin.setTotalCount(count);
		pagin.adjustPageNo();
		List<ProHotProductShow> products = Collections.emptyList();
		if (count > 0) {
			products = proHotProductMapper.selectByCondition(pagin.getFirstResult(), pagin.getPageSize(), condition);
		}

		// 设置价格
		List<Integer> productIds = getProductIdsOfVO(products);
		List<ProSku> skus = proSkuMapper.selectPrice(productIds);
		Map<Integer, ProSku> idSku = convertSkuListToMap(skus);

		for (ProHotProductShow show: products) {
			ProSku sku = idSku.get(show.getProductId());
			if (sku != null) {
				show.setHotPrice(sku.getHotPrice());
				show.setMemberPrice(sku.getMemberPrice());
			}
		}

		pagin.setList(products);

		return pagin;
	}

	private Map<Integer, ProSku> convertSkuListToMap(List<ProSku> skus) {
		Map<Integer, ProSku> idNameMap = new HashMap<>();
		for (ProSku sku: skus) {
			idNameMap.put(sku.getProductId(), sku);
		}
		return idNameMap;
	}

	private List<Integer> getProductIdsOfVO(List<ProHotProductShow> products) {
		List<Integer> ids = new ArrayList<>();
		for (ProHotProductShow show: products) {
			ids.add(show.getProductId());
		}
		return ids;
	}

	//添加爆品管理
	@Override
	public void addHotProduct(ProHotFloor proHotFloor) {
		proHotFloorMapper.insert(proHotFloor);
		
	}

	@Override
	public List<Integer> getFloors() {
		List<Integer> floors=proHotFloorMapper.getFloors();
		return floors;
	}

	@Override
	public Pagination findCenProduct(int pageno, int pagesize, Map<String, String> params) {
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<Map<String, Object>> productList = proHotFloorMapper.selectBaseProduct(params);

		// 设置价格
		List<Integer> productIds = getIdOfMap(productList);
		List<ProSku> skus = proSkuMapper.selectPrice(productIds);
		Map<Integer, ProSku> idSku = convertSkuListToMap(skus);

		for (Map<String, Object> prodMap: productList) {
			ProSku sku = idSku.get(prodMap.get("id"));
			if (sku != null) {
				prodMap.put("hotPrice", String.valueOf(sku.getHotPrice()));
				prodMap.put("memberPrice", String.valueOf(sku.getMemberPrice()));
			}
		}

		Pagination p = new Pagination(startPage, productList);
		p.setList(productList);
		p.setParams(params);
		return p;
	}

	private List<Integer> getIdOfMap(List<Map<String, Object>> mapList) {
		List<Integer> ids = new ArrayList<>();
		if (CollectionUtil.isEmpty(mapList)) {
			return ids;
		}
		for (Map<String, Object> entry: mapList) {
			if (entry.containsKey("id")) {
				ids.add((Integer) entry.get("id"));
			}
		}
		return ids;
	}

	@Override
	public void saveHotFloor(ProHotFloor hotFloor) {
		if(hotFloor != null) {
			if(hotFloor.getId() == null) {
				proHotFloorMapper.insertSelective(hotFloor);
			} else {
				proHotFloorMapper.updateByPrimaryKeySelective(hotFloor);
			}
		}
	}

	@Override
	public void insertHotProduct(ProHotProduct hotProduct) {
		if(hotProduct != null) {
			proHotProductMapper.insertSelective(hotProduct);
		}
	}

	@Override
	public ProHotProduct getHotProduct(int floorId, int productId) {
		ProHotProduct hotProduct = new ProHotProduct();
		hotProduct.setFloorId(floorId);
		hotProduct.setProductId(productId);
		return proHotProductMapper.selectOne(hotProduct);
	}

	@Override
	public ProHotFloor getHotFloorById(int floorId) {
		return proHotFloorMapper.selectByPrimaryKey(floorId);
	}

	@Override
	public ProHotFloor getHotFloorAndProducts(Integer floorId) {
		ProHotFloor hotFloor = getHotFloorById(floorId);
		List<ProHotProduct> hotProductList = findHotProductByFloorId(floorId);
		hotFloor.setHotProductList(hotProductList);
		return hotFloor;
	}

	@Override
	public List<ProHotProduct> findHotProductByFloorId(int floorId) {
		return proHotProductMapper.findHotProductByFloorId(floorId);
	}

	@Override
	public void deleteHotFloorById(int floorId) {
		//删除爆品楼层
		proHotFloorMapper.deleteByPrimaryKey(floorId);
		ProHotProduct hotProduct=new ProHotProduct();
		hotProduct.setFloorId(floorId);
		List<ProHotProduct> hotProductIdList=proHotProductMapper.select(hotProduct);
		for(ProHotProduct product:hotProductIdList){
			Integer hotProductId=product.getId();
			ProShopHotProduct psh=new ProShopHotProduct();
			psh.setHotProductId(hotProductId);
			proShopHotProductMapper.delete(psh);
		}
		//删除爆品
		proHotProductMapper.delete(hotProduct);
	}

	@Override
	public void saveHotShopProduct(List<ProShopHotProduct> products) {
		// 先清空之前爆品
		proShopHotProductMapper.deleteAll();
		// 然后批量插入
		proShopHotProductMapper.insertBatch(products);
	}

	@Override
	public List<ProShopHotProductVO> getHotShopProducts() {
		List<ProShopHotProduct> hotProducts = proShopHotProductMapper.selectAllAndProductName();
		if (CollectionUtil.isEmpty(hotProducts)) {
			return Collections.emptyList();
		}

		List<ProShopHotProductVO> productVOList = new ArrayList<>();
		for (ProShopHotProduct product: hotProducts) {
			ProShopHotProductVO productVO = new ProShopHotProductVO();
			BeanUtils.copyProperties(product, productVO);
			productVOList.add(productVO);
		}
		return productVOList;
	}

	private Map<Integer, String> convertToMap(List<Product> products) {
		Map<Integer, String> idNameMap = new HashMap<>();
		for (Product product: products) {
			idNameMap.put(product.getId(), product.getName());
		}
		return idNameMap;
	}

	private List<Integer> getProductIds(List<ProShopHotProduct> hotProducts) {
		List<Integer> ids = new ArrayList<>();
		for (ProShopHotProduct product: hotProducts) {
			ids.add(product.getHotProductId());
		}
		return ids;
	}

	@Override
	public List<ProHotFloor> listProHotFloor() {
		List<ProHotFloor> allFloor = proHotFloorMapper.selectAllAndCategoryName();
		return allFloor;
	}

	/**
	 * 查询出被删除商品的 ID
	 * @return 爆品表的 ID
     */
	private List<Integer> listDeletedHotProductIds(int hotFloorId, List<ProHotProduct> list) {
		List<Integer> hotProductIds = getHotProductIds(list);
		return proHotProductMapper.selectDeletedIds(hotFloorId, hotProductIds);
	}

	private List<Integer> getHotProductIds(List<ProHotProduct> list) {
		List<Integer> ids = new ArrayList<>();
		for (ProHotProduct proHotProduct :list) {
			if (proHotProduct.getId() != null) {
				ids.add(proHotProduct.getId());
			}
		}
		return ids;
	}

	/**
	 * 删除首页爆品中的已被删除的爆品
     */
	private void deleteProShopHotProducts(List<Integer> deletedProductIds) {
		if (CollectionUtil.isEmpty(deletedProductIds)) {
			return;
		}
		// 删除爆品
		proHotProductMapper.deleteByIds(deletedProductIds);
		// 删除首页爆品
		proShopHotProductMapper.deleteByHotProductIds(deletedProductIds);
	}

	/**
	 * 对有 ID 的商品进行更新
	 * 判断哪些商品被删除了，同步到商品首页删除
	 * 对于没有 ID 的进行新增
     */
	@Override
	public void updateHotFloor(ProHotFloor hotFloor) {
		// 楼层进行更新
		saveHotFloor(hotFloor);
		//商品更新
		List<ProHotProduct> list = hotFloor.getHotProductList();
		// 查询删除了的商品对爆品表的 ID
		List<Integer> deletedProductIds = listDeletedHotProductIds(hotFloor.getId(), list);
		// 删除商城爆品
		deleteProShopHotProducts(deletedProductIds);
		//先清空该楼层的所有爆品
		for(ProHotProduct hotProduct : list) {
			hotProduct.setFloorId(hotFloor.getId());
			if (hotProduct.getId() == null) {
				insertHotProduct(hotProduct);
			} else {
				proHotProductMapper.updateByPrimaryKey(hotProduct);
			}
		}
	}

	@Override
	public String isHotProductInIndex(Integer floorId) {
		List<ProHotProduct> hotProducts = findHotProductByFloorId(floorId);
		List<Integer> indexHotProductIds = listShopHotProductIds();
		if (CollectionUtil.isEmpty(hotProducts) || CollectionUtil.isEmpty(indexHotProductIds)) {
			return null;
		}
		int sum = 0;
		for (ProHotProduct proHotProduct :hotProducts) {
			if (indexHotProductIds.contains(proHotProduct.getId())) {
				sum++;
			}

		}
		if (sum == 0) {
			return null;
		}
		
		return String.format("该楼层有 %d 个商品在首页爆品中，会被同步删除", sum);
	}

	@Override
	public String isHotProductInOtherFloor(Integer floorId, List<Integer> productIds) {
		if (CollectionUtil.isEmpty(productIds)) {
			return "请选择商品";
		}
		if(Sets.newHashSet(productIds).size() < productIds.size()) {
			return "请不要在一个楼层添加重复的商品";
		}
		List<ProHotProduct> hotProducts = proHotProductMapper.selectOtherFloor(floorId, productIds);
		if (CollectionUtil.isEmpty(hotProducts)) {
			return null;
		}
		StringBuilder strHotProductNames = new StringBuilder();
		for (ProHotProduct proHotProduct :hotProducts) {
			strHotProductNames.append(proHotProduct.getProductName());
		}

		return String.format("商品 %s 已设置在了其他楼层，不能重复设置", strHotProductNames);
	}

	@Override
	public List<Integer> listShopHotProductIds() {
		List<ProShopHotProduct> shopHotProducts = proShopHotProductMapper.selectAll();
		List<Integer> shopHotProductIds = new ArrayList<>();
		for (ProShopHotProduct proShopHotProduct :shopHotProducts) {
			shopHotProductIds.add(proShopHotProduct.getHotProductId());
		}

		return shopHotProductIds;
	}
}


